package com.zhongbei.controller;

import com.zhongbei.pojo.Activity;
import com.zhongbei.service.ActivityService;
import com.zhongbei.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @PostMapping
    public Result<String> create(@RequestBody Activity activity) {
        int i = activityService.insertActivity(activity);
        return i == 1 ? Result.success("新增成功") : Result.error("新增失败");
    }

    @DeleteMapping("/{activityId}")
    public Result<String> delete(@PathVariable Integer activityId) {
        int i = activityService.deleteActivityByActivityId(activityId);
        return i == 1 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping("/{activityId}")
    public Result<String> update(@PathVariable Integer activityId, @RequestBody Activity activity) {
        activity.setActivityId(activityId);
        int i = activityService.updateActivityByActivityId(activity);
        return i == 1 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping
    public Result<List<Activity>> listAll() {
        return Result.success(activityService.selectActivity());
    }

    @GetMapping("/{activityId}")
    public Result<List<Activity>> getById(@PathVariable int activityId) {
        return Result.success(activityService.selectActivityByActivityId(activityId));
    }

    @GetMapping("/branch/{branchId}")
    public Result<List<Activity>> getByBranch(@PathVariable int branchId) {
        return Result.success(activityService.selectActivityByBranchId(branchId));
    }

    @GetMapping("/member/{memberId}")
    public Result<List<Activity>> getByMember(@PathVariable int memberId) {
        return Result.success(activityService.selectActivityByMemberId(memberId));
    }

    @GetMapping("/member/{memberId}/branch/{branchId}")
    public Result<List<Activity>> getByMemberAndBranch(@PathVariable int memberId, @PathVariable int branchId) {
        return Result.success(activityService.selectActivityByMemberIdAndBranchId(memberId, branchId));
    }
}
